<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="供应商名称" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入供应商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="supplierUserName">
        <el-input
          v-model="queryParams.supplierUserName"
          placeholder="请输入联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mall:gift:supplier:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-refresh"
          size="mini"
          v-hasPermi="['mall:gift:supplier:syn']"
          @click="handleSync"
          :loading="btnLoad"
          >同步</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="dataList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="供应商简称" align="center" prop="supplierName" />
      <el-table-column
        label="供应商全称"
        align="center"
        prop="supplierFullName"
      />
      <!-- <el-table-column
        label="营业执照号"
        align="center"
        prop="supplierCertificateNumber"
      />
      <el-table-column
        label="供应商地址"
        align="center"
        prop="supplierAddress"
      /> -->
      <el-table-column label="是否可用" align="center" prop="isStatus">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isStatus"
            :active-value="1"
            :inactive-value="0"
            @change="handleIsStatus(scope.row)"
          />
        </template>
      </el-table-column>
      <!-- <el-table-column label="联系人" align="center" prop="supplierUserName" />
      <el-table-column label="联系电话" align="center" prop="supplierMobile" /> -->

      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mall:gift:supplier:query']"
            >修改</el-button
          >
          <!-- 删除 -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mall:gift:supplier:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="供应商简称" prop="supplierName">
          <el-input
            v-model="form.supplierName"
            placeholder="请输入供应商简称"
          />
        </el-form-item>
        <el-form-item label="供应商全称" prop="supplierFullName">
          <el-input
            v-model="form.supplierFullName"
            placeholder="请输入供应商全称"
          />
        </el-form-item>
        <el-form-item label="营业执照号" prop="supplierCertificateNumber">
          <el-input
            v-model="form.supplierCertificateNumber"
            placeholder="请输入营业执照号"
          />
        </el-form-item>
        <el-form-item label="供应商地址" prop="supplierAddress">
          <el-input
            v-model="form.supplierAddress"
            placeholder="请输入供应商地址"
          />
        </el-form-item>
        <el-form-item label="经营范围" prop="supplierBusinessScope">
          <el-checkbox-group v-model="form.supplierBusinessScope">
            <el-checkbox :label="1">商城配送</el-checkbox>
            <el-checkbox :label="2">到店自提</el-checkbox>
            <el-checkbox :label="3">券核销</el-checkbox>
            <el-checkbox :label="4">实物商品</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="联系人" prop="supplierUserName">
          <el-input
            v-model="form.supplierUserName"
            placeholder="请输入联系人"
          />
        </el-form-item>
        <el-form-item label="联系电话" prop="supplierMobile">
          <el-input
            v-model="form.supplierMobile"
            maxlength="11"
            placeholder="请输入联系电话"
          />
        </el-form-item>
        <!-- <el-form-item label="登录用户名" prop="userName" v-if="form.id == null">
          <el-input
            v-model="form.userName"
            autocomplete="new-password"
            placeholder="请输入登录用户名"
          />
        </el-form-item>
        <el-form-item label="登录密码" prop="password" v-if="form.id == null">
          <el-input
            v-model="form.password"
            autocomplete="new-password"
            type="password"
            maxlength="20"
            show-password
            placeholder="请输入登录密码"
          />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="submitLoading" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  supplierList,
  addSupplier,
  updateSupplier,
  deleteSupplier,
  getSupplier,
  updateIsStatus,
  synPlatSupplier,
} from "@/api/company/supplier";
export default {
  name: "Supplier",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 提交按钮loading
      submitLoading: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        postCode: undefined,
        postName: undefined,
        status: undefined,
      },
      // 表单参数
      form: {
        supplierBusinessScope: [],
      },
      // 表单校验
      rules: {
        supplierName: [
          { required: true, message: "供应商简称不能为空", trigger: "blur" },
        ],
        supplierFullName: [
          { required: true, message: "供应商全称不能为空", trigger: "blur" },
        ],
        supplierBusinessScope: [
          { required: true, message: "经营范围不能为空", trigger: "blur" },
        ],
      },
      btnLoad: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //同步供应商
    handleSync() {
      this.btnLoad = true;
      synPlatSupplier()
        .then((res) => {
          this.btnLoad = false;
          this.$modal.msgSuccess("同步成功");
          this.getList();
        })
        .catch((err) => {
          this.btnLoad = false;
        });
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      supplierList(this.queryParams).then((response) => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        supplierBusinessScope: [],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.postId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 是否可用
    handleIsStatus(row) {
      const { id, isStatus } = row;
      console.log(isStatus);
      let text = isStatus == 1 ? "启用" : "停用";
      this.$modal
        .confirm("确认要" + text + "吗？")
        .then(function () {
          return updateIsStatus({ id, isStatus });
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.isStatus = row.isStatus == 0 ? 1 : 0;
        });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getSupplier(id).then((response) => {
        this.form = {
          ...response.data,
          supplierBusinessScope: response.data.supplierBusinessScope
            ? response.data.supplierBusinessScope.split(",").map(Number)
            : [],
        };
        console.log(this.form);
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.submitLoading = true;
          const data = {
            ...this.form,
            supplierBusinessScope: this.form.supplierBusinessScope.join(","),
          };
          if (this.form.id != undefined) {
            updateSupplier(data)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
                this.submitLoading = false;
              })
              .catch(() => {
                this.submitLoading = false;
              });
          } else {
            addSupplier(data)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
                this.submitLoading = false;
              })
              .catch(() => {
                this.submitLoading = false;
              });
          }
        }
      });
    },
    // 删除
    handleDelete(row) {
      this.$modal.confirm("是否确认删除？").then(() => {
        deleteSupplier(row.id).then((response) => {
          this.$modal.msgSuccess("删除成功");
          this.getList();
        });
      });
    },
  },
};
</script>
